//
//  NavigationViewController.swift
//  ContactPoster
//
//  Created by TSYH on 2023/11/20.
//

import UIKit

open class NavigationController: UINavigationController, UIGestureRecognizerDelegate, UINavigationControllerDelegate {
    open override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.isHidden = true
        view.backgroundColor = .white
        delegate = self
        interactivePopGestureRecognizer?.delegate = self
    }
    
    open override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if !viewControllers.isEmpty {
            // 二级页面隐藏Tabbar, 限第一次push
            if viewControllers.count == 1 {
                viewController.hidesBottomBarWhenPushed = true
            }
        } else {
            viewController.hidesBottomBarWhenPushed = false
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    open func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
        if let topVC = topViewController as? BaseViewController, !topVC.shouldPopWhenClickBackItem {
            topVC.handleNavigationBarBackBlockAction()
            return false
        }
        return true
    }
    
    // MARK: UIGestureRecognizerDelegate
    open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if let topVC = topViewController as? BaseViewController, !topVC.shouldPopWhenClickBackItem {
            topVC.handleNavigationBarBackBlockAction()
            return false
        }
        return viewControllers.count > 1
    }
    
    // MARK: UINavigationControllerDelegate
    open func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        
        viewController.transitionCoordinator?.notifyWhenInteractionChanges({ context in
            guard !context.isCancelled else { return }
            let fromVC = context.viewController(forKey: .from)
            if let fromVC = fromVC as? BaseViewController {
                fromVC.handlerActionAfterPopFinished()
            }
        })
    }
}

// 转场动画
extension NavigationController: UIViewControllerTransitioningDelegate {
    
    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if let transitionController = presented as? TransitioningProtocol {
            return transitionController.openTransition
        }
        return nil
    }
    
    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if let transitionController = dismissed as? TransitioningProtocol {
            return transitionController.closeTransition
        }
        return nil
    }
}

public protocol TransitioningProtocol {
    var openTransition: UIViewControllerAnimatedTransitioning { get }
    var closeTransition: UIViewControllerAnimatedTransitioning { get }
}
